package com.xiaobawang.knowledge.spider.advice;

import com.xiaobawang.knowledge.spider.exception.BusinessException;
import com.xiaobawang.knowledge.spider.model.dto.ResponseDTO;
import com.xiaobawang.knowledge.spider.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private final EmailService emailService;

    public GlobalExceptionHandler(EmailService emailService) {
        this.emailService = emailService;
    }

    /**
     * @author 18101119
     * @description 统一异常
     * @date 2024/10/23 16:43
     * @param ex 统一异常
     * @return com.chinalife.devc.smartpark.service.model.dto.base.ResponseDTO<java.lang.String>
     */
    @ExceptionHandler(Throwable.class)
    public ResponseDTO<String> handleException(Throwable ex) {
        log.error("业务逻辑异常", ex);
//        this.emailService.sendErrEmail();
        return ResponseDTO.error(500, MDC.get("traceId"));
    }

    /**
     * @author 18101119
     * @description 统一异常
     * @date 2024/10/23 16:43
     * @param ex 统一异常
     * @return com.chinalife.devc.smartpark.service.model.dto.base.ResponseDTO<java.lang.String>
     */
    @ExceptionHandler(Exception.class)
    public ResponseDTO<String> handleException(Exception ex) {
        log.error("业务逻辑异常", ex);
//        this.emailService.sendErrEmail();
        return ResponseDTO.error(500, MDC.get("traceId"));
    }

    /**
     * @author synhard
     * @id 18101119
     * @description 业务异常
     * @date 2024/3/25 17:06
     * @param ex 业务异常
     * @return com.chinalife.cdp.entity.ResponseInfo<java.lang.String>
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseDTO<String> handleBusinessException(BusinessException ex) {
        log.error("业务逻辑异常", ex);
//        this.emailService.sendErrEmail();
        return ResponseDTO.error(500, ex.getErrMsg(), MDC.get("traceId"));
    }
}
